home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / Basic Classes / Z Headers / ZString.h < prev    next >
Encoding:
Text File  |  1997-02-13  |  3.3 KB  |  107 lines  |  [TEXT/CWIE]

  1. //    ZString.h
  2. //    11/12/96
  3.  
  4. #ifndef __ZSTRING_H
  5. #define __ZSTRING_H
  6.  
  7. #include <Types.h>    // for Str255 type (used for Toolbox calls)
  8.  
  9. class ZString;
  10.  
  11. // a comparison function analogous to the ANSI strcmp:
  12. short JStrcmp( const ZString&, const ZString& );
  13.  
  14. class ZString 
  15. {
  16.     public:
  17.         Str255    buf;        // here's the actual data member
  18.       
  19.         //------ constructors & destructor ------
  20.         
  21.         ZString( void ) { buf[0]=0; }
  22.         ZString( const ZString& pStr ) { buf[0]=0; *this = pStr; }
  23.         ZString( const char* pChars ) { buf[0]=0; *this = pChars; }
  24.  
  25.         //------ assignment operators ------
  26.         
  27.         ZString& operator= ( const ZString& pStr );
  28.         ZString& operator= ( const Str255 pPstr );
  29.         ZString& operator= ( const char* pChars );
  30.  
  31.         //------ conversion operators & functions ------
  32.         
  33.         ZString( const long pNum );        // convert int to string
  34.         ZString( const Str255 pPstr ) { buf[0]=0; *this = pPstr; }
  35.         operator ConstStr255Param(void) const { return buf; }
  36.  
  37.         void StuffChars( char* pChars, const int maxlen=255 );
  38.         void StuffPstr( unsigned char* pPstr, const int maxlen=255 );
  39.         
  40.         // I should probably make these guys into member functions:
  41.         
  42.         virtual  long        IntValue( const ZString& );
  43.         virtual  ZString    Plural( const ZString& );
  44.         virtual  ZString    Uppercase( const ZString& );
  45.  
  46.         //------ comparison operators ------
  47.  
  48.         Boolean operator== ( const ZString& s ) const
  49.             { return JStrcmp(*this, s) == 0; }
  50.         
  51.         Boolean operator!= ( const ZString& s ) const
  52.             { return JStrcmp(*this, s) != 0; }
  53.         
  54.         Boolean operator> ( const ZString& s ) const
  55.             { return JStrcmp(*this, s) > 0; }
  56.         
  57.         Boolean operator< ( const ZString& s ) const
  58.             { return JStrcmp(*this, s) < 0; }
  59.         
  60.         Boolean operator>= ( const ZString& s ) const
  61.             { return JStrcmp(*this, s) >= 0; }
  62.         
  63.         Boolean operator<= ( const ZString& s ) const
  64.             { return JStrcmp(*this, s) <= 0; }
  65.  
  66.         //------ length, array-access, substrings ------
  67.         
  68.         short Length(void) const { return buf[0]; }
  69.         
  70.         char& operator[] ( const int i )
  71.             { return (i>=0 && i < buf[0] ? buf[i] : buf[1]); }
  72.  
  73.         // position of a substring (first character is 1)
  74.         int Index( const ZString& pSubstr ) const;
  75.  
  76.         // return a particular element (elements separated by char delimiters)
  77.         ZString Element( const int pElem, const char pDelim=' ' ) const;
  78.  
  79.         // return a substring including 'from' but not 'to' -- again, first char is 1
  80.         ZString Substr( const int from, const int to ) const;
  81.         ZString operator() ( const int from, const int to ) const { return Substr(from,to); }
  82.         
  83.         // shortcuts for specific substrings:
  84.         ZString Left( const int qty=1 ) const { return Substr(1,qty+1); }
  85.         ZString Right( const int qty=1 ) const { return Substr(Length()-qty+1,999); }
  86.  
  87.         //------ get modified versions of the string ------
  88.         ZString Trim() const;            // remove leading/trailing whitespace
  89.         ZString Compress() const;        // compress multiple spaces/tabs to one space
  90.         
  91.         //------ string concatenation ------
  92.         ZString& operator+= ( const ZString& s );
  93.         ZString operator+ ( const ZString& s2 ) const
  94.             { ZString out(*this); out += s2; return out; }
  95.  
  96.         //------ multiplication, e.g., "\pfoo" * 3 == "\pfoofoofoo" ------
  97.         ZString& operator*= ( const int times )
  98.             { ZString what(*this);
  99.               for (int i=1; i<times; i++) *this += what;
  100.               return *this; }
  101.         
  102.         ZString operator* ( const int times )
  103.             { ZString out(*this); out *= times; return out; }
  104. };
  105.  
  106. #endif
  107.